package com.example.qrcode.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qrcode.entity.SceneBindingAi;
import com.example.qrcode.mapper.SceneBindingAiMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/sceneBindingAi")
@RequiredArgsConstructor
public class SceneBindingAiAdminController {

    private final SceneBindingAiMapper sceneBindingAiMapper;
    @GetMapping("/test")
    public String test() {
        return "Hello World!SceneBindingAiAdminController";
    }
    @GetMapping("/page")
    public Page<SceneBindingAi> page(@RequestParam(defaultValue = "1") long pageNum,
                                     @RequestParam(defaultValue = "10") long pageSize,
                                     @RequestParam(required = false) String deviceCode,
                                     @RequestParam(required = false) String graphicName) {
        Page<SceneBindingAi> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SceneBindingAi> qw = new QueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            qw.like("device_code", deviceCode);
        }
        if (graphicName != null && !graphicName.isEmpty()) {
            qw.eq("graphic_name", graphicName);
        }
        qw.orderByDesc("update_time");
        return sceneBindingAiMapper.selectPage(page, qw);
    }

    @GetMapping("/{id}")
    public SceneBindingAi get(@PathVariable Long id) {
        return sceneBindingAiMapper.selectById(id);
    }

    @PostMapping
    public int create(@RequestBody SceneBindingAi body) {
        return sceneBindingAiMapper.insert(body);
    }

    @PutMapping
    public int update(@RequestBody SceneBindingAi body) {
        return sceneBindingAiMapper.updateById(body);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Long id) {
        return sceneBindingAiMapper.deleteById(id);
    }

    @PostMapping("/batchDelete")
    public int batchDelete(@RequestBody List<Long> ids) {
        return sceneBindingAiMapper.deleteBatchIds(ids);
    }
}

